package com.combining.e_composite.main;

import com.combining.e_composite.Goose;
import com.combining.e_composite.face.AbstractDuckFactory;
import com.combining.e_composite.face.Quackable;
import com.combining.e_composite.impl.CountingDuckFactory;
import com.combining.e_composite.impl.Flock;
import com.combining.e_composite.impl.GooseAdapter;
import com.combining.e_composite.impl.QuackCounter;

/**
 * 组合模式允许我们像对待单个对象一样对待对象集合。还有什么模式能比组合模式创建一群Quackable更好呢
 * 
 * @author dell
 * 
 */
// 又是鸭子，又是鹅，又是Quackable的，我们有管理上的困扰。所以我们需要使用组合模式，将许多Quackable集结成一个群。
public class DuckSimulator {

	public static void main(String[] args) {
		DuckSimulator simulator = new DuckSimulator();
		AbstractDuckFactory duckFactory = new CountingDuckFactory();

		simulator.simulate(duckFactory);
	}

	void simulate(AbstractDuckFactory duckFactory) {
		Quackable redheadDuck = duckFactory.createRedheadDuck();
		Quackable duckCall = duckFactory.createDuckCall();
		Quackable rubberDuck = duckFactory.createRubberDuck();
		Quackable gooseDuck = new GooseAdapter(new Goose());

		System.out.println("\n鸭子模拟器，和鹅适配器，和装饰者，和工厂，和组合：");

		Flock flockOfDucks = new Flock();

		flockOfDucks.add(redheadDuck);
		flockOfDucks.add(duckCall);
		flockOfDucks.add(rubberDuck);
		flockOfDucks.add(gooseDuck);

		Flock flockOfMallards = new Flock();

		Quackable mallardOne = duckFactory.createMallardDuck();
		Quackable mallardTwo = duckFactory.createMallardDuck();
		Quackable mallardThree = duckFactory.createMallardDuck();
		Quackable mallardFour = duckFactory.createMallardDuck();

		flockOfMallards.add(mallardOne);
		flockOfMallards.add(mallardTwo);
		flockOfMallards.add(mallardThree);
		flockOfMallards.add(mallardFour);

		flockOfDucks.add(flockOfMallards);

		System.out.println("\nDuck Simulator: Whole Flock Simulation");
		simulate(flockOfDucks);

		System.out.println("\nDuck Simulator: Mallard Flock Simulation");
		simulate(flockOfMallards);

		System.out.println("鸭子共叫了" + QuackCounter.getQuacks() + "次");
	}

	void simulate(Quackable duck) {
		duck.quack();
	}
}
